package com.yingsheng.bi.hks.service.comm;

import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import com.yingsheng.bi.hks.basic.consts.MsgDict;
import com.yingsheng.bi.hks.basic.enums.SmsRegionEnum;
import com.yingsheng.bi.hks.basic.exception.ServiceRuntimeException;
import com.yingsheng.bi.hks.config.props.ChannelSmsProperties;
import com.yingsheng.bi.hks.config.props.SmsProperties;
import com.yingsheng.bi.hks.dto.SmsCodeGetBeforeLoginReq;
import com.yingsheng.bi.hks.dto.SmsCodeGetRes;
import com.yingsheng.bi.hks.dto.SmsSendReq;
import com.yingsheng.bi.hks.dto.SmsSendRes;
import com.yingsheng.bi.hks.repository.redis.SubjectRedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 短信服务
 *
 * @author root
 */
@Slf4j
@Service
public class SmsBiService {

    @Autowired
    private ChannelSmsProperties channelSmsProperties;

    @Autowired
    private SubjectRedisService subjectRedisService;

    public SmsProperties getSmsProperties(String channel) {
        return this.channelSmsProperties.getSmsProperties(channel);
    }

    public SmsCodeGetRes get(SmsCodeGetBeforeLoginReq req) {
        String channel = req.getChannel();
        String mobile = req.getMobile();
        String scene = req.getScene();
        if (subjectRedisService.smsSendLimitCheck(channel, mobile, scene)) {
            throw new ServiceRuntimeException(MsgDict.SMS_REQUEST_FREQUENTLY);
        }
        String code = subjectRedisService.genSmsCode(channel, mobile, scene);
        SmsSendReq smsSendReq = new SmsSendReq();
        smsSendReq.setChannel(req.getChannel());
        smsSendReq.setMobile(req.getMobile());
        smsSendReq.setScene(req.getScene());
        smsSendReq.setParam(code);
        SmsSendRes smsSendRes = send(smsSendReq);
        return new SmsCodeGetRes();
    }

    public SmsSendRes send(SmsSendReq req) {
        if (!StringUtils.hasText(req.getTemplateId())) {
            String templateId = channelSmsProperties.getTemplateId(req.getChannel(), req.getScene());
            if (!StringUtils.hasText(templateId)) {
                throw new ServiceRuntimeException(MsgDict.SMS_TEMPLATE_NONNULL);
            }
            req.setTemplateId(templateId);
        }
        SmsProperties smsProperties = getSmsProperties(req.getChannel());
        Credential cred = new Credential(smsProperties.getSecretId(), smsProperties.getSecretKey());
        SmsClient client = new SmsClient(cred, SmsRegionEnum.HD.getValue());
        SendSmsRequest sendSmsRequest = new SendSmsRequest();
        sendSmsRequest.setSmsSdkAppId(smsProperties.getAppId());
        sendSmsRequest.setSignName(smsProperties.getSignName());
        sendSmsRequest.setTemplateId(req.getTemplateId());
        sendSmsRequest.setPhoneNumberSet(req.getMobiles());
        sendSmsRequest.setTemplateParamSet(req.getParams());
        sendSmsRequest.setSessionContext(req.getUserId());
        SmsSendRes res = null;
        /**
         * {
         *     "Response": {
         *         "SendStatusSet": [
         *             {
         *                 "SerialNo": "5000:1045710669157053657849499619",
         *                 "PhoneNumber": "+8618501234444",
         *                 "Fee": 1,
         *                 "SessionContext": "outsid_1729495320_1011",
         *                 "Code": "Ok",
         *                 "Message": "send success",
         *                 "IsoCode": "CN"
         *             },
         *             {
         *                 "SerialNo": "5000:1045710669157053657849499718",
         *                 "PhoneNumber": "+8618501234445",
         *                 "Fee": 1,
         *                 "SessionContext": "outsid_1729495320_1011",
         *                 "Code": "Ok",
         *                 "Message": "send success",
         *                 "IsoCode": "CN"
         *             }
         *         ],
         *         "RequestId": "a0aabda6-cf91-4f3e-a81f-9198114a2279"
         *     }
         * }
         */
        try {
            SendSmsResponse sendSmsResponse = client.SendSms(sendSmsRequest);
            log.info("sms send res [{}]", SendSmsResponse.toJsonString(sendSmsResponse));
            res = new SmsSendRes();
            res.setRequestId(sendSmsResponse.getRequestId());
        } catch (TencentCloudSDKException e) {
            log.error(e.getMessage(), e);
            throw new ServiceRuntimeException(e.getMessage());
        }
        return res;
    }


}
